package team.fourth.combat.web;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import team.fourth.combat.commen.entity.Result;
import team.fourth.combat.commen.utils.RedisUtil;
import team.fourth.combat.pojo.Setmeal;
import team.fourth.combat.service.SetmealService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController2 {

    @Reference
    SetmealService setmealService;

    @Autowired
    RedisUtil redisUtil;

//    @RequestMapping("/getSetmeal")
//    public Result getSetmeal(HttpServletRequest request){
//        //假设我这个接口需要判断用户有没有登录，登录了才能查看套餐列表
//        String token = request.getHeader("token");
//        //1、判断token是否有值
//        if(token == null){
//            return Result.error("请登录");
//        }
//        //2、根据token去获取用户信息
//        String memberJson = redisUtil.get(token);
//        Member member = JSON.parseObject(memberJson, Member.class);
//        //3、如果没有获取到用户信息那就需要告诉用户登录
//        if(member == null){
//            return Result.error("请登录");
//        }
//        List<Setmeal> setmeals = setmealService.getSetmeal();
//        return Result.success("",setmeals);
//    }

    @RequestMapping("/getSetmeal")
    public Result getSetmeal(){
        List<Setmeal> setmeals = setmealService.getSetmeal();
        return Result.success("",setmeals);
    }

    public static void main(String[] args) {
        String url = "http://127.0.0.1:84/pages/setmeal_detail.html?id=12&name=jack&age=19";
        String id = getParamByUrl(url, "id");
        String name = getParamByUrl(url, "name");
        String age = getParamByUrl(url, "age");

        System.out.println(id);
        System.out.println(name);
        System.out.println(age);
    }


    private static String getParamByUrl(String url,String k){
        String[] urlArray = url.split("\\?");//["http://127.0.0.1:84/pages/setmeal_detail.html","id=12&name=jack&age=19"]

        String paramStr =  urlArray[1];//"id=12&name=jack&age=19"

        String[] paramArray = paramStr.split("&");//["id=12","name=jack","age=19","idabc":"1231"]

        for (String param : paramArray) {
            String key = param.split("=")[0];//["id","12"]
            if(k.equals(key)){
                String value = param.split("=")[1];
                return value;
            }

        }
        return "";
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        Setmeal setmeal = setmealService.findByIdBatch(id);
        return Result.success("",setmeal);
    }


    @RequestMapping("/getToken")
    public Result getToken(){
        String token = UUID.randomUUID().toString();
        redisUtil.setex(token,token,60 * 5);
        return Result.success("",token);
    }


    @RequestMapping("/findDetailById")
    public Result findDetailById(Integer id){
        Setmeal setmeal = setmealService.findDetailById(id);
        return Result.success("",setmeal);
    }



}
